<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>改变this指向-call</title>
</head>

<body>
  <script>
    // ============================== 改变this指向 - call ============================== 
    const obj = { name: '佩奇' }

    // call() 作用： 第一个调用函数  第二改变this指向
    function fun(x, y) {
      console.log(this)
      console.log(x+y)
      console.log(arguments)  //所有实参
    }
    fun(1,2)  //x+y=3
    //call方法的第一个参数obj是用来指定函数内的this指向
    //第二个到最后的都是实参
    fun.call(obj,10,20)   //x+y=30

    
    
    
    
    
    
    // 2. call的应用场景 - 检测数据类型
     // 2.1 typeof 检测数据类型不够精确的
    // 2.2 Object.prototype.toString()  返回的结果是[object xxx类型]

     console.log(Object.prototype.toString())                 //[object Object]
     console.log(Object.prototype.toString.call(1))           //[object Number]
     console.log(Object.prototype.toString.call(`1`))         //[object String]
     console.log(Object.prototype.toString.call(null))        //[object Null]
     console.log(Object.prototype.toString.call(undefined))   //[object Undefined]
     console.log(Object.prototype.toString.call(/a/))         //[object RegExp]
     console.log(Object.prototype.toString.call([]))          //[object Array]
     console.log(Object.prototype.toString.call(new Date))    //[object Date]

    //简易封装

    function getType(data) {
      return Object.prototype.toString.call(data)
    }





   
  </script>
</body>

</html>